package com.qyhl.module_activities.act.signup;


import android.annotation.SuppressLint;
import android.util.Log;

import com.qyhl.module_activities.R;
import com.qyhl.module_activities.common.ActUrl;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.commonlib.entity.act.ActivityViewBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created  on 2017/12/25.
 *
 * @author helin
 */
public class SignUpActModel implements SignUpActContract.SignUpActModel {

    private final SignUpActContract.SignUpActPresenter presenter;

    SignUpActModel(SignUpActContract.SignUpActPresenter presenter) {
        this.presenter = presenter;

    }

    @Override
    public void getActView(int actId) {

        EasyHttp.get(ActUrl.ACTIVITY_VIEW)
                .params("actId", String.valueOf(actId))
                .execute(new SimpleCallBack<List<ActivityViewBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.onError(2, ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                    }

                    @Override
                    public void onSuccess(List<ActivityViewBean> activityViewBeans) {
                        presenter.onSuccess(activityViewBeans);
                    }
                });

    }





    @Override
    public void getQiuniuToken(boolean isUpload) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url =CommonUtils.getInstance().getCircleUrl() + "?timestamp=" + time;


        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=getUpToken&type=3"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean upTokenBean) {
                            presenter.getTokenSuccess(upTokenBean, isUpload);
                    }
                });

    }


    @Override
    public void uploadData(HashMap<String, String> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = ActUrl.ACTIVITY_SIGN_ENCRYPTION + "?timestamp=" + time;
        StringBuilder sb = new StringBuilder();
        for (String key : params.keySet()) {
            sb.append(key);
            sb.append("=");
            sb.append(params.get(key));
            sb.append("&");
        }
        sb.deleteCharAt(sb.length() - 1);

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, sb.toString()))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        Log.i("@@@@",e.toString());
                        presenter.uplodError(-1, ContextUtilts.getInstance().getmContext().getString(R.string.error_sign));
                    }

                    @Override
                    public void onSuccess(String s) {
                        presenter.signSuccess();
                    }
                });
    }

    @Override
    public void checkSign(String actId) {
        String userName = CommonUtils.getInstance().getUserName();
        EasyHttp.post(ActUrl.ACTIVITY_IS_SIGN)
                .params("userName", userName)
                .params("actId", actId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.checkError(ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                    }

                    @Override
                    public void onSuccess(String s) {
                        presenter.checkSuccess();
                    }
                });
    }


}
